
'use strict';

import React, {Component} from 'react';

import {
    View,
    Text,
    StyleSheet,
    Platform,
    AppRegistry
} from 'react-native';

import RefreshListView,{RefreshState} from '../../base/widget/RefreshListView';
import FindHouseCell from './FindHouseCell';
import FindHouseApi from "./api/FindHouseApi";
import {DIALOG_LOADING} from "../../base/Constants";
import BaseScene from "../../base/BaseScene";

let _page = 1;//当前第几页
const PAGESIZE=10;//item的个数
let isReload = true;
let isFirstIn = true;

export default class FindHouseScene extends BaseScene {

    constructor(props) {
        super(props);
        this.state = {
            dataList: [],
            response:'',
            refreshState: RefreshState.Idle,
        }
    }

    componentWillUnmount(){

    }

    componentDidMount () {
        this._onHeaderRefresh();
    }


    _onHeaderRefresh = () => {
        if(!isFirstIn){
            this.setState({refreshState: RefreshState.HeaderRefreshing})
        }
        isReload = true
        _page = 1;
        this._getDataList()
    }

    _onFooterRefresh = () => {
        this.setState({refreshState: RefreshState.FooterRefreshing})
        isReload = false
        _page = _page+1;
        this._getDataList()
    }

    _requestSuccess = (response) =>{
        isFirstIn = false;
        let backList = response.obj;
        let newList = isReload ? backList : [...this.state.dataList, ...backList]
        this.setState({
            response: response,
            dataList : newList,
            refreshState: backList.length<10 ? RefreshState.NoMoreData : RefreshState.Idle
        })
    }

    _requestFailed = (response) =>{
        this.setState({
            response: response,
            refreshState: RefreshState.Idle
        })
    }

    _onReloadClick=()=>{
        this._getDataList()
    }

    // 获取测试数据
    _getDataList(){
        let params = {'dataFrom':'APP','page':_page,'pageSize':PAGESIZE,'cityCode':'0571'}
        this._fetchRequest({...FindHouseApi.getHouseList,isFirstIn:isFirstIn},params);
    }

    _keyExtractor = (item, index) => {
        return index;
    }

    _renderCell = (info) => {
        return <FindHouseCell info={info.item} />
    }

    _getChildView(){
        return (<View style={styles.container}>
            <RefreshListView
                data={this.state.dataList}
                keyExtractor={this._keyExtractor}
                renderItem={this._renderCell}
                refreshState={this.state.refreshState}
                onHeaderRefresh={this._onHeaderRefresh}
                onFooterRefresh={this._onFooterRefresh}

                // 可选
                footerRefreshingText= '玩命加载中 >.<'
                footerFailureText = '我擦嘞，居然失败了 =.=!'
                footerNoMoreDataText= '-我是有底线的-'
            />
        </View>)
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        marginTop: Platform.OS == 'ios' ? 20 : 0,
    },
    title: {
        fontSize: 18,
        height: 84,
        textAlign: 'center'
    }
})